package org.jeecg.modules.crm.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.crm.entity.CrmContact;
import org.jeecg.modules.crm.service.ICrmContactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Description: 客户联系人
 * @Author: jeecg-boot
 * @Date:   2023-06-03
 * @Version: V1.0
 */
@Api(tags="客户联系人")
@RestController
@RequestMapping("/crm/contact")
@Slf4j
public class CrmContactController extends JeecgController<CrmContact, ICrmContactService> {
    @Autowired
    private ICrmContactService contactService;
    
    /**
     * 分页列表查询
     *
     * @param contact
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "客户联系人-分页列表查询")
    @ApiOperation(value="客户联系人-分页列表查询", notes="客户联系人-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(CrmContact contact,
                                @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                HttpServletRequest req) {
        QueryWrapper<CrmContact> queryWrapper = QueryGenerator.initQueryWrapper(contact, req.getParameterMap());
        Page<CrmContact> page = new Page<CrmContact>(pageNo, pageSize);
        IPage<CrmContact> pageList = contactService.page(page, queryWrapper);
        return Result.OK(pageList);
    }
    
    /**
     * 根据客户ID获取联系人列表
     *
     * @param customerId
     * @return
     */
    @AutoLog(value = "客户联系人-根据客户ID获取联系人列表")
    @ApiOperation(value="客户联系人-根据客户ID获取联系人列表", notes="客户联系人-根据客户ID获取联系人列表")
    @GetMapping(value = "/getByCustomerId")
    public Result<?> getByCustomerId(@RequestParam(name="customerId", required=true) String customerId) {
        List<CrmContact> contactList = contactService.getContactsByCustomerId(customerId);
        return Result.OK(contactList);
    }
    
    /**
     * 设置主要联系人
     *
     * @param id 联系人ID
     * @param customerId 客户ID
     * @return
     */
    @AutoLog(value = "客户联系人-设置主要联系人")
    @ApiOperation(value="客户联系人-设置主要联系人", notes="客户联系人-设置主要联系人")
    @PutMapping(value = "/setPrimary")
    public Result<?> setPrimary(@RequestParam(name="id", required=true) String id,
                              @RequestParam(name="customerId", required=true) String customerId) {
        boolean result = contactService.setPrimaryContact(id, customerId);
        if (result) {
            return Result.OK("设置成功!");
        } else {
            return Result.error("设置失败!");
        }
    }
    
    /**
     * 添加
     *
     * @param contact
     * @return
     */
    @AutoLog(value = "客户联系人-添加")
    @ApiOperation(value="客户联系人-添加", notes="客户联系人-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody CrmContact contact) {
        contactService.save(contact);
        return Result.OK("添加成功！");
    }
    
    /**
     * 编辑
     *
     * @param contact
     * @return
     */
    @AutoLog(value = "客户联系人-编辑")
    @ApiOperation(value="客户联系人-编辑", notes="客户联系人-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody CrmContact contact) {
        contactService.updateById(contact);
        return Result.OK("编辑成功!");
    }
    
    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "客户联系人-通过id删除")
    @ApiOperation(value="客户联系人-通过id删除", notes="客户联系人-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id", required=true) String id) {
        contactService.removeById(id);
        return Result.OK("删除成功!");
    }
    
    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "客户联系人-批量删除")
    @ApiOperation(value="客户联系人-批量删除", notes="客户联系人-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids", required=true) String ids) {
        this.contactService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }
    
    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "客户联系人-通过id查询")
    @ApiOperation(value="客户联系人-通过id查询", notes="客户联系人-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id", required=true) String id) {
        CrmContact contact = contactService.getById(id);
        if(contact==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(contact);
    }
    
    /**
     * 导出excel
     *
     * @param request
     * @param contact
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CrmContact contact) {
        return super.exportXls(request, contact, CrmContact.class, "客户联系人");
    }
    
    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CrmContact.class);
    }
    
    /**
     * 根据客户ID查询联系人列表
     * @param customerId 客户ID
     * @return 联系人列表
     */
    @GetMapping(value = "/list/{customerId}")
    @ApiOperation(value = "根据客户ID查询联系人列表", notes = "根据客户ID查询联系人列表")
    public Result<List<CrmContact>> getContactsByCustomerId(@PathVariable(name = "customerId") String customerId) {
        log.info("根据客户ID查询联系人列表, 客户ID: {}", customerId);
        List<CrmContact> contactList = contactService.getContactsByCustomerId(customerId);
        return Result.OK(contactList);
    }
} 